import requests
from lxml import etree

if __name__ == '__main__':
    # 1、 获取热门城市的城市名称 全部城市的名称
    # headers = {
    #     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
    # }
    # url= 'https://www.aqistudy.cn/historydata/'
    # page_text = requests.get(url=url, headers=headers).text
    #
    # tree = etree.HTML(page_text)
    # hot_li_list = tree.xpath('//div[@class="bottom"]/ul/li')
    # hot_city_names = []
    # all_city_names = []
    # # 解析热门城市的城市名称
    # for li in hot_li_list:
    #     hot_city_name = li.xpath('./a/text()')[0]
    #     hot_city_names.append(hot_city_name)
    #
    # # 解析的是全部c城市
    # city_name_list = tree.xpath('//div[@class="bottom"]/ul/div[2]/li')
    # for li in city_name_list:
    #     city_name = li.xpath('./a/text()')[0]
    #     all_city_names.append(city_name)
    # print(hot_city_names, all_city_names)

    # 2、 解析到热门城市和所有城市对应的a标签
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
    }
    url= 'https://www.aqistudy.cn/historydata/'
    page_text = requests.get(url=url, headers=headers).text

    tree = etree.HTML(page_text)
    # 解析到热门城市和所有城市对应的a标签
    # div/ul/li/a  热门城市a标签的层级关系
    # div/ul/div[2]/li/a 全部 城市a标签的层级关系
    a_list = tree.xpath('//div[@class="bottom"]/ul/li/a | //div[@class="bottom"]/ul/div[2]/li/a')
    all_city_names = []
    for a in a_list:
        city_name = a.xpath('./text()')[0]
        all_city_names.append(city_name)
    print(all_city_names, len(all_city_names))